// ==========================================================================
// 云捷GO自动生成控制器相关代码，只生成一次，按需修改,再次生成不会覆盖.
// 生成日期：2021-07-26 15:16:14
// 生成路径: app/controller/module/news/news_controller.go
// 生成人：gfcms
// ==========================================================================
package news

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"myblog/app/model"
	categoryModel "myblog/app/model/module/category"
	newsModel "myblog/app/model/module/news"
	categoryService "myblog/app/service/module/category"
	newsService "myblog/app/service/module/news"
	shopService "myblog/app/service/module/shop"
	userService "myblog/app/service/system/user"
	"myblog/app/utils/response"
	"os"
	"strconv"
	"time"
)

//列表页
func List(r *ghttp.Request) {
	response.BuildTpl(r, "module/news/list.html").WriteTpl()
}

//列表分页数据
func ListAjax(r *ghttp.Request) {
	var req *newsModel.SelectPageReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		response.ErrorResp(r).SetMsg(err.Error()).Log("news管理", req).WriteJsonExit()
	}
	rows := make([]newsModel.EntityExtend, 0)
	result, page, err := newsService.SelectListByPage(req)

	if err == nil && len(result) > 0 {
		rows = result
	}
	response.BuildTable(r, page.Total, rows).WriteJsonExit()
}

//新增页面
func Add(r *ghttp.Request) {
	pid := r.GetQueryInt64("pid")
	var pmenu categoryModel.EntityExtend
	pmenu.Id = 0
	pmenu.Catname = "主目录"

	tmp, err := categoryService.SelectRecordById(pid)
	if err == nil && tmp != nil && tmp.Id > 0 {
		pmenu.Id = tmp.Id
		pmenu.Catname = tmp.Catname
	}
	//获取店铺名称
	shopname, err := shopService.GetShopnameId()
	if err != nil {
		response.ErrorResp(r).SetBtype(model.Buniss_Add).SetMsg(err.Error()).WriteJsonExit()
	}
	response.BuildTpl(r, "module/news/add.html").WriteTpl(g.Map{
		"menu":     pmenu,
		"shopname": shopname,
	})
}

//新增页面保存
func AddSave(r *ghttp.Request) {
	var req *newsModel.AddReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		response.ErrorResp(r).SetBtype(model.Buniss_Add).SetMsg(err.Error()).Log("news新增数据", req).WriteJsonExit()
	}
	//保存栏目图片
	if file := r.GetUploadFile("thumb"); file != nil {
		user := userService.GetProfile(r.Session)
		curDir, err := os.Getwd()
		if err != nil {
			response.ErrorResp(r).SetBtype(model.Buniss_Edit).SetMsg(err.Error()).Log("保存栏目图片", g.Map{"userid": user.UserId}).WriteJsonExit()
		}
		saveDir := curDir + "/public/upload/" //文件保存路径

		curdate := time.Now().UnixNano()
		file.Filename = strconv.FormatInt(curdate, 10) + ".png"
		name, err := file.Save(saveDir, true)
		if err != nil {
			r.Response.WriteExit(err)
		}
		req.Thumb = "/upload/" + name
	} else {
		req.Thumb = ""
	}
	id, err := newsService.AddSave(req, r.Session)

	if err != nil || id <= 0 {
		response.ErrorResp(r).SetBtype(model.Buniss_Add).Log("news新增数据", req).WriteJsonExit()
	}
	response.SucessResp(r).SetData(id).Log("news新增数据", req).WriteJsonExit()
}

//修改页面
func Edit(r *ghttp.Request) {
	id := r.GetQueryInt64("id")
	if id <= 0 {
		response.ErrorTpl(r).WriteTpl(g.Map{
			"desc": "参数错误",
		})
		return
	}
	entity, err := newsService.SelectRecordById(id)
	var pmenu categoryModel.EntityExtend
	// 获取栏目名称
	tmp, err := categoryService.SelectRecordById(int64(entity.Catid))
	if err == nil && tmp != nil && tmp.Id > 0 {
		pmenu.Id = tmp.Id
		pmenu.Catname = tmp.Catname
	}
	if err != nil || entity == nil {
		response.ErrorTpl(r).WriteTpl(g.Map{
			"desc": "数据不存在",
		})
		return
	}
	response.BuildTpl(r, "module/news/edit.html").WriteTpl(g.Map{
		"news":     entity,
		"menu":     pmenu,
	})
}

//修改页面保存
func EditSave(r *ghttp.Request) {
	var req *newsModel.EditReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		response.ErrorResp(r).SetBtype(model.Buniss_Edit).SetMsg(err.Error()).Log("news修改数据", req).WriteJsonExit()
	}
	//保存栏目图片
	if file := r.GetUploadFile("thumb"); file != nil {
		user := userService.GetProfile(r.Session)
		curDir, err := os.Getwd()
		if err != nil {
			response.ErrorResp(r).SetBtype(model.Buniss_Edit).SetMsg(err.Error()).Log("保存栏目图片", g.Map{"userid": user.UserId}).WriteJsonExit()
		}
		saveDir := curDir + "/public/upload/" //文件保存路径

		curdate := time.Now().UnixNano()
		file.Filename = strconv.FormatInt(curdate, 10) + ".png"
		name, err := file.Save(saveDir, true)
		if err != nil {
			r.Response.WriteExit(err)
		}
		req.Thumb = "/upload/" + name
	} else {
		req.Thumb = req.Oldimage
	}
	rs, err := newsService.EditSave(req, r.Session)

	if err != nil || rs <= 0 {
		response.ErrorResp(r).SetBtype(model.Buniss_Edit).SetMsg(err.Error()).Log("news修改数据", req).WriteJsonExit()
	}
	response.SucessResp(r).SetBtype(model.Buniss_Edit).Log("news修改数据", req).WriteJsonExit()
}

//删除数据
func Remove(r *ghttp.Request) {
	var req *model.RemoveReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		response.ErrorResp(r).SetBtype(model.Buniss_Del).SetMsg(err.Error()).Log("news删除数据", req).WriteJsonExit()
	}

	rs := newsService.DeleteRecordByIds(req.Ids)

	if rs > 0 {
		response.SucessResp(r).SetBtype(model.Buniss_Del).Log("news删除数据", req).WriteJsonExit()
	} else {
		response.ErrorResp(r).SetBtype(model.Buniss_Del).Log("news删除数据", req).WriteJsonExit()
	}
}

//导出
func Export(r *ghttp.Request) {
	var req *newsModel.SelectPageReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		response.ErrorResp(r).Log("news导出数据", req).WriteJsonExit()
	}
	url, err := newsService.Export(req)

	if err != nil {
		response.ErrorResp(r).SetBtype(model.Buniss_Other).Log("news导出数据", req).WriteJsonExit()
	}
	response.SucessResp(r).SetBtype(model.Buniss_Other).SetMsg(url).WriteJsonExit()
}

//上传
func Upfile(r *ghttp.Request) {
	files := r.GetUploadFiles("file")
	user := userService.GetProfile(r.Session)
	fipath, err := newsService.Upfile(files)
	if err != nil {
		response.ErrorResp(r).SetBtype(model.Buniss_Edit).SetMsg(err.Error()).Log("上传文件", g.Map{"userid": user.UserId}).WriteJsonExit()
	}
	response.SucessResp(r).SetBtype(model.Buniss_Other).SetData(fipath).WriteJsonExit()
}

//删除
func Delfile(r *ghttp.Request) {
	url := r.GetString("url")
	user := userService.GetProfile(r.Session)
	err := newsService.Delfile(url)
	if err != nil {
		response.ErrorResp(r).SetBtype(model.Buniss_Edit).SetMsg(err.Error()).Log("上传文件", g.Map{"userid": user.UserId}).WriteJsonExit()
	}
	response.SucessResp(r).SetBtype(model.Buniss_Other).SetData(g.Map{"code": "sccess"}).WriteJsonExit()
}

